package com.zxn.loadingdemo;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class ImageRotateActivity extends AppCompatActivity {

    public static void jumpTo(Context context) {
        Intent intent = new Intent(context, ImageRotateActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_image_rotate);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        ImageView ivImageView = findViewById(R.id.ivImageView);
        //startRotateAnimation(ivImageView);
        startObjectAnimator(ivImageView);

    }

    private void startObjectAnimator(ImageView ivImageView) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(ivImageView, "rotation", 0, 360);
        animator.setDuration(800); // 动画持续时间（毫秒）
        animator.setRepeatCount(ObjectAnimator.INFINITE); // 动画无限重复
        animator.setRepeatMode(ObjectAnimator.RESTART); // 动画重复模式
        animator.start();
    }

    private void startRotateAnimation(ImageView ivImageView) {
        RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(1088);
        rotateAnimation.setRepeatCount(Animation.INFINITE);
        ivImageView.startAnimation(rotateAnimation);
        rotateAnimation.start();
    }


}